import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/cupertino.dart';
import 'package:lcapp/data/popover_data_entity.dart';
import 'package:lcapp/model/share/custom_share.dart';
import 'package:lcapp/routes/route.dart';

class BannerOperate {
  //banner跳转处理
  static void bannerGoPage(PopoverDataEntity data, {Function? callBack}) async {
    print('banner url ==== ' + data.link!);
    if(data.link == null){
      return;
    }
      //判断是否为应用内跳转
      if (data.link!.startsWith('http')) {
        Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed('H5Page', arguments: {
          'url': data.link,
        }).then((value){
          if(callBack!=null){
            callBack();
          }
        });
      }else if (data.link!.startsWith('local://')) {
        Uri u = Uri.parse(data.link!);
        String routeNmae = u.queryParameters['routeName'].toString();
        String arguments = u.queryParameters['arguments'].toString();
        if(arguments==null||arguments=='null'){
          Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed(routeNmae).then((value){
            if(callBack!=null){
              callBack();
            }
          });
          return;
        }
        Uint8List base64deBody = base64Decode(arguments);
        String result = Utf8Decoder().convert(base64deBody);
        print('arguments===' + result.toString());
        String type = u.queryParameters['type'].toString();
        try{
          Map map = json.decode(result);
          Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed(routeNmae, arguments: map).then((value){
            if(callBack!=null){
              callBack();
            }
          });
        }catch(e){
          if(type == '3'){
            Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed(routeNmae, arguments: int.parse(result)).then((value){
              if(callBack!=null){
                callBack();
              }
            });
          }else if(type == '4'){
            Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed(routeNmae, arguments: int.parse(result)).then((value){
              if(callBack!=null){
                callBack();
              }
            });
          }else if(type == '5'){
            Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed(routeNmae, arguments: int.parse(result)).then((value){
              if(callBack!=null){
                callBack();
              }
            });
          }else{
            Navigator.of(ASRRoute.navigatorKey.currentContext!).pushNamed(routeNmae, arguments: result).then((value){
              if(callBack!=null){
                callBack();
              }
            });
          }
        }

      }
      else if (data.link!.startsWith('mini://')) {
        Uri u = Uri.parse(data.link!);
        String? path = u.queryParameters['path'];
        String? userName = u.queryParameters['userName'];
        // CustomShare().WXOpenMiniProgram(userName: userName??'', path: path??'');
      }else if (data.link!.startsWith('wxchat://')) {
        Uri u = Uri.parse(data.link!);
        String? path = u.queryParameters['path'];
        String? corpId = u.queryParameters['corpId'];
        // CustomShare().WXChatService(corpId: corpId??'', path: path??"");
    }
  }
}
